﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Portal.Utility;
using Portal.Web.Utility;
using Portal.Web.Model;
using Portal.Exam.Bussiness;
using Portal.Exam.Model;
using Portal.Core;

namespace Portal.Exam.Render
{
    public class AddQuestion 
    {
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut(); 
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);

                string RootGroupQuestionId = (string)WebEnvironments.Request(SiteId, "RootGroupQuestionId");
                string GroupQuestionId = (string)WebEnvironments.Request(SiteId, "GroupQuestionId");
                if (string.IsNullOrEmpty(GroupQuestionId)) throw new Exception("Tham số không hợp lệ!");
                GroupQuestionCls OGroupQuestion = GroupQuestionBll.CreateGroupQuestionModel(SiteId, GroupQuestionId);
                if (OGroupQuestion == null) throw new Exception("Tham số không hợp lệ!");

                string IconRoot = WebConfig.GetButtonIconRoot(OSiteParam);
                string SelectType =
                             "<select id=drpSelectType class=drplist>\r\n" +
                             "   <option value=\"BestAnswer\">Câu hỏi chọn phương án đúng nhất</option>\r\n" +
                             "   <option value=\"Multichoice\">Câu hỏi chọn 1 hoặc nhiều phương án trả lời</option>\r\n" +
                             "   <option value=\"TrueFalse\">Câu hỏi đúng sai</option>\r\n" +
                             "   <option value=\"CrossLink\">Câu hỏi ghép hợp</option>\r\n" +
                             "   <option value=\"Image\">Câu hỏi dạng hình ảnh</option>\r\n" +
                             "   <option value=\"FreeText\">Câu hỏi luận</option>\r\n" +                             
                             "</select>\r\n";

                string SelectLevel =
                    "<select id=drpSelectLevelQuestion class=drplist>\r\n" +
                    "   <option value=\"0\">Mức độ dễ</option>\r\n"+
                    "   <option value=\"1\">Mức độ trung bình</option>\r\n" +
                    "   <option value=\"2\">Mức độ khó</option>\r\n" +
                    "</select>\r\n";
                string RetHtml =
                    " <script language=javascript> \r\n" +
                    " function ClientSaveAddQuestion() \r\n" +
                    " { \r\n" +
                    "     SiteId='"+SiteId+"';\r\n"+                    
                    "     Question= '';\r\n"+ 
                    "     Subject = document.getElementById('txtSubject').value;\r\n"+
                    "     MaxPoint=document.getElementById(\"txtPoint\").value; \r\n" +
                    "     RootGroupQuestionId='"+RootGroupQuestionId+"';\r\n"+
                    "     GroupQuestionId='"+GroupQuestionId+"';\r\n"+
                    "     SelectLevel = document.getElementById(\"drpSelectLevelQuestion\").value; \r\n" +
                    "     SelectType = document.getElementById(\"drpSelectType\").value; \r\n" +
                    "     AjaxOut=Portal.Exam.Render.AddQuestion.ServerSideSaveAddQuestion(SiteId, RootGroupQuestionId, GroupQuestionId, Subject, Question,MaxPoint, SelectLevel, SelectType).value; \r\n" +
                    "     if(AjaxOut.Error) \r\n" +
                    "     { \r\n" +
                    "         alert(AjaxOut.InfoMessage); \r\n" +
                    "         return; \r\n" +
                    "     } \r\n" +
                    "     window.open(AjaxOut.RetUrl,'_self');\r\n"+
                    " } \r\n" +

                    "</script>\r\n" +

                " <div class=DatabaseFunction>\r\n" +
                "        <ul>\r\n" +
                "            <li onclick=\"javascript:ClientSaveAddQuestion();\"><img class=image src=\"" + IconRoot + "/Save.png\"><div>Ghi lại</div></li>\r\n" +
                "            <li onclick=\"javascript:window.open('GroupQuestionTreeView.aspx?RId="+RootGroupQuestionId+"','_self');\"><img class=image src=\"" + IconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                "        </ul>\r\n" +
                "  </div>\r\n" +
                "   <h3>Tạo mới câu hỏi vào nhóm - "+OGroupQuestion.GroupQuestionName+"</h3>\r\n"+
                "   <div class=CssFormSaveUpdate>\r\n" +
                "      <table style=\"width:99%\">" +
                "          <tr>" +
                "              <td class=\"Col1\">Loại câu hỏi</td>" +
                "              <td>" + SelectType + "</td>" +
                "          </tr>" +
                "          <tr>" +
                "              <td class=\"Col1\">Mức độ</td>" +
                "              <td>" + SelectLevel + "</td>" +
                "          </tr>" +
                "          <tr>" +
                "              <td class=\"Col1\">Tiêu đề:</td>" +
                "              <td><input type=textbox class=textbox id=\"txtSubject\"></td>" +
                "          </tr>" +               
                "          <tr>" +
                "              <td class=\"Col1\">Điểm:</td>" +
                "              <td><input class=textbox id=txtPoint value=\"1\"></td>" +
                "          </tr>" +
                //"          <tr>" +
                //"              <td class=\"Col1\">Thời gian(giây):</td>" +
                //"              <td><input class=textbox id=txtSecondTime value=\"30\"></td>" +
                //"          </tr>" +
                //"          <tr>" +
                //"              <td class=\"Col1\">Câu hỏi trí nhớ:</td>" +
                //"              <td><input class=checkbox type=checkbox id=chkIsMemory></td>" +
                //"          </tr>" +
                //"          <tr>" +
                //"              <td class=\"Col1\">Thời gian ẩn:</td>" +
                //"              <td><input class=textbox id=txtSecondHidden value=\"30\"></td>" +
                //"          </tr>" +
                //"          <tr>" +
                //"              <td></td>" +
                //"              <td><input type=button class=button onclick=javascript:ClientSaveAddNewBestAnswerQuestion(); value=\"Ghi lại\"><input type=button class=button  onclick=\"javascript:window.history.back(-1);\" value=\"Quay lại\"></td>" +
                //"          </tr>" +
                "   </table>" +
                "</div>\r\n";
                OAjaxOut.HtmlContent = RetHtml;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage= ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod]
        public AjaxOut ServerSideSaveAddQuestion(
            string SiteId, 
            string RootGroupQuestionId,
            string GroupQuestionId, 
            string Subject,
            string Question,
            string MaxPoint,
            string SelectLevel,
            string SelectType)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();

                if (string.IsNullOrEmpty(Subject))
                {
                    throw new Exception("Chưa nhập tiêu đề câu hỏi");
                }
                if (FunctionUtilities.checkInteger(MaxPoint) == false) throw new Exception("Dữ liệu điểm nhập không hợp lệ! Phải nhập kiểu số!");
                Question = Question.Trim();
                QuestionCls OQuestion = new QuestionCls();
                OQuestion.Title = Subject;
                OQuestion.Question = Question;
                OQuestion.MaxPoint = int.Parse(MaxPoint);
                OQuestion.Level = int.Parse(SelectLevel);
                OQuestion.Type = SelectType;
                OQuestion.GroupQuestionId = GroupQuestionId;
                OQuestion.QuestionId = System.Guid.NewGuid().ToString();
                
                string QuestionId = new QuestionBll().AddQuestion(null, SiteId, GroupQuestionId, SelectType, int.Parse(SelectLevel), OQuestion, 1);
                string Url = "UpdateQuestion.aspx?QuestionId=" + QuestionId + "&GroupQuestionId=" + GroupQuestionId + "&RootGroupQuestionId=" + RootGroupQuestionId;
                OAjaxOut.RetUrl = Url;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage= ex.Message.ToString();
                OAjaxOut.Error = true;
            }
            return OAjaxOut;
        }
    }
}